//
//  TopicViewController.m
//  Leisure
//
//  Created by Alex on 16/6/30.
//  Copyright © 2016年 蓝鸥科技有限公司西安分公司. All rights reserved.
//

#import "TopicViewController.h"

@interface TopicViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;
@property (weak, nonatomic) IBOutlet UIScrollView *scroll;
@property (nonatomic,strong)UISegmentedControl *segentedmControl;
@property (nonatomic,strong)NSMutableArray *hotDataSource;
@property (nonatomic,strong)NSMutableArray *newestDataSource;
@property (nonatomic,assign)NSInteger newestStart;
@property (nonatomic,assign)NSInteger hotStart;

@end

@implementation TopicViewController

- (UISegmentedControl *)segentedmControl {
    
    if (!_segentedmControl) {
        _segentedmControl = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
        [_segentedmControl insertSegmentWithTitle:@"HOT" atIndex:0 animated:YES];
        [_segentedmControl insertSegmentWithTitle:@"NEW" atIndex:1 animated:YES];
        [_segentedmControl addTarget:self action:@selector(segentedmControlAction:) forControlEvents:UIControlEventValueChanged];
    }
    return _segentedmControl;
}

- (NSMutableArray *)newestDataSource {
    
    if (!_newestDataSource) {
        _newestDataSource = [NSMutableArray array];
    }
    return _newestDataSource;
}

- (NSMutableArray *)hotDataSource {
    
    if (!_hotDataSource) {
        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   
    self.navigationItem.titleView = self.segentedmControl;
    
    [self.newestTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"TopicModel_cell"];
    
    [self.hotTableView registerNib:[UINib nibWithNibName:@"TopicCell" bundle:nil] forCellReuseIdentifier:@"TopicModel_cell"];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self newestRequestWithStart:0 limit:10];
    [self hotRequestWithStart:0 limit:10];
    
    self.newestTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{

        [self.newestDataSource removeAllObjects];
        [self newestRequestWithStart:0 limit:10];
    }];
    
    self.newestTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        self.newestStart += 10;
        [self newestRequestWithStart:self.newestStart limit:10];
        
    }];

    self.hotTableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{

        [self.hotDataSource removeAllObjects];
        [self hotRequestWithStart:0 limit:10];
    }];
    
    self.hotTableView.mj_footer = [MJRefreshBackGifFooter footerWithRefreshingBlock:^{
        
        self.hotStart += 10;
        [self hotRequestWithStart:self.hotStart limit:10];
        
    }];
    
}

- (void)newestRequestWithStart:(NSInteger)start limit:(NSInteger)limit {
    
    [RequestManager fecthDataWithUrl:kTopicUrl parameter:@{@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        
        
        for (NSDictionary *smallDic in bigdic[@"data"][@"list"]) {
            
            TopicModel *model = [[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.newestDataSource addObject:model];
            
        }
    
        
    } updateUI:^{
        [self.newestTableView.mj_header endRefreshing];
        [self.newestTableView.mj_footer endRefreshing];
        [self.newestTableView reloadData];
    }];
    
}

- (void)hotRequestWithStart:(NSInteger)start limit:(NSInteger)limit {
    
    [RequestManager fecthDataWithUrl:kTopicUrl parameter:@{@"start":@(start),@"limit":@(limit),@"sort":@"hot",} mehtod:@"POST" compelet:^(NSData *data) {
        
        NSDictionary *bigdic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (NSDictionary *smallDic in bigdic[@"data"][@"list"]) {
            
            TopicModel *model = [[TopicModel alloc]init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.hotDataSource addObject:model];
            
        }
        
    } updateUI:^{
        [self.hotTableView.mj_header endRefreshing];
        [self.hotTableView.mj_footer endRefreshing];
        [self.hotTableView reloadData];
        
    }];
    
}

- (void)segentedmControlAction:(UISegmentedControl *)sender {
    
    if (sender.selectedSegmentIndex == 0) {
        
        [self.scroll setContentOffset:CGPointZero animated:YES];
    } else {
        
        [self.scroll setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    if (scrollView == self.scroll) {
        
        if (self.scroll.contentOffset.x == 0) {
            
            self.segentedmControl.selectedSegmentIndex = 0;
            
        } else if (self.scroll.contentOffset.x == kWidth) {
            
            self.segentedmControl.selectedSegmentIndex = 1;
        }
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (tableView == self.hotTableView) {
        
        return self.hotDataSource.count;
    }else {
        
        return self.newestDataSource.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TopicModel *model = [[TopicModel alloc]init];
    if (tableView == self.hotTableView) {
        
        model = self.hotDataSource[indexPath.row];
    } else {
        
        model = self.newestDataSource[indexPath.row];
    }
    
    BaseCell *cell = [FactoryMode productCellWithModel:model tableView:tableView indexPath:indexPath];
 
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 230;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    TopicModel *model = [[TopicModel alloc]init];
    if (tableView == self.hotTableView) {
        
        model = self.hotDataSource[indexPath.row];
    } else {
        
        model = self.newestDataSource[indexPath.row];
    }
    TopicListViewController *topicList = [[TopicListViewController alloc]init];
    topicList.contentID = model.contentid;
    [self.navigationController pushViewController:topicList animated:YES];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}



@end
